package org.jf.tool.fx.jftoolfx.document.controller;

import atlantafx.base.theme.Styles;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import org.jf.tool.fx.jftoolfx.common.BaseController;
import org.jf.tool.fx.jftoolfx.document.bean.Ascii;
import org.jf.tool.fx.jftoolfx.document.bean.Ascii;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author jifeng_zhang
 * @date 2025/10/16 下午7:20
 */
public class AsciiController extends BaseController {
    @FXML
    public TableView asciiTableView;
    @FXML
    public TextField asciiTextField;

    private static final ObservableList<Ascii> TABLE_DATA = FXCollections.observableArrayList();

    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {
        TABLE_DATA.clear();
        TABLE_DATA.addAll(
                new Ascii(0, "NUT"),
                new Ascii(1, "SOH"),
                new Ascii(2, "STX"),
                new Ascii(3, "ETX"),
                new Ascii(4, "EOT"),
                new Ascii(5, "ENQ"),
                new Ascii(6, "ACK"),
                new Ascii(7, "BEL"),
                new Ascii(8, "BS"),
                new Ascii(9, "TAB"),
                new Ascii(10, "LF"),
                new Ascii(11, "VT"),
                new Ascii(12, "FF"),
                new Ascii(13, "CR"),
                new Ascii(14, "SO"),
                new Ascii(15, "SI"),
                new Ascii(16, "DLE"),
                new Ascii(17, "DC1"),
                new Ascii(18, "DC2"),
                new Ascii(19, "DC3"),
                new Ascii(20, "DC4"),
                new Ascii(21, "NAK"),
                new Ascii(22, "SYN"),
                new Ascii(23, "ETB"),
                new Ascii(24, "CAN"),
                new Ascii(25, "EM"),
                new Ascii(26, "SUB"),
                new Ascii(27, "ESC"),
                new Ascii(28, "FS"),
                new Ascii(29, "GS"),
                new Ascii(30, "RS"),
                new Ascii(31, "US"),
                new Ascii(32, "(space)"),
                new Ascii(33, "!"),
                new Ascii(34, "\""),
                new Ascii(35, "#"),
                new Ascii(36, "$"),
                new Ascii(37, "%"),
                new Ascii(38, "&"),
                new Ascii(39, "'"),
                new Ascii(40, "("),
                new Ascii(41, ")"),
                new Ascii(42, "*"),
                new Ascii(43, "+"),
                new Ascii(44, ","),
                new Ascii(45, "-"),
                new Ascii(46, "."),
                new Ascii(47, "/"),
                new Ascii(48, "0"),
                new Ascii(49, "1"),
                new Ascii(50, "2"),
                new Ascii(51, "3"),
                new Ascii(52, "4"),
                new Ascii(53, "5"),
                new Ascii(54, "6"),
                new Ascii(55, "7"),
                new Ascii(56, "8"),
                new Ascii(57, "9"),
                new Ascii(58, ":"),
                new Ascii(59, ";"),
                new Ascii(60, "<"),
                new Ascii(61, "="),
                new Ascii(62, ">"),
                new Ascii(63, "?"),
                new Ascii(64, "@"),
                new Ascii(65, "A"),
                new Ascii(66, "B"),
                new Ascii(67, "C"),
                new Ascii(68, "D"),
                new Ascii(69, "E"),
                new Ascii(70, "F"),
                new Ascii(71, "G"),
                new Ascii(72, "H"),
                new Ascii(73, "I"),
                new Ascii(74, "J"),
                new Ascii(75, "K"),
                new Ascii(76, "L"),
                new Ascii(77, "M"),
                new Ascii(78, "N"),
                new Ascii(79, "O"),
                new Ascii(80, "P"),
                new Ascii(81, "Q"),
                new Ascii(82, "R"),
                new Ascii(83, "S"),
                new Ascii(84, "T"),
                new Ascii(85, "U"),
                new Ascii(86, "V"),
                new Ascii(87, "W"),
                new Ascii(88, "X"),
                new Ascii(89, "Y"),
                new Ascii(90, "Z"),
                new Ascii(91, "["),
                new Ascii(92, "/"),
                new Ascii(93, "]"),
                new Ascii(94, "^"),
                new Ascii(95, "_"),
                new Ascii(96, "`"),
                new Ascii(97, "a"),
                new Ascii(98, "b"),
                new Ascii(99, "c"),
                new Ascii(100, "d"),
                new Ascii(101, "e"),
                new Ascii(102, "f"),
                new Ascii(103, "g"),
                new Ascii(104, "h"),
                new Ascii(105, "i"),
                new Ascii(106, "j"),
                new Ascii(107, "k"),
                new Ascii(108, "l"),
                new Ascii(109, "m"),
                new Ascii(110, "n"),
                new Ascii(111, "o"),
                new Ascii(112, "p"),
                new Ascii(113, "q"),
                new Ascii(114, "r"),
                new Ascii(115, "s"),
                new Ascii(116, "t"),
                new Ascii(117, "u"),
                new Ascii(118, "v"),
                new Ascii(119, "w"),
                new Ascii(120, "x"),
                new Ascii(121, "y"),
                new Ascii(122, "z"),
                new Ascii(123, "{"),
                new Ascii(124, "|"),
                new Ascii(125, "}"),
                new Ascii(126, "~"),
                new Ascii(127, "DEL")
        );
        asciiTableView.setItems(TABLE_DATA);
        Styles.toggleStyleClass(asciiTableView, Styles.STRIPED);
        Styles.toggleStyleClass(asciiTableView, Styles.DENSE);
        Styles.toggleStyleClass(asciiTableView, Styles.BORDERED);
    }

    public void queryAscii(ActionEvent actionEvent) {
        ObservableList<Ascii> data = FXCollections.observableArrayList();
        String text = asciiTextField.getText();
        if (text == null || text.isEmpty()) {
            asciiTableView.setItems(TABLE_DATA);
            return;
        }
        for (Ascii tableDatum : TABLE_DATA) {
            if (tableDatum.getName().toString().contains(text)) {
                data.add(tableDatum);
            }
        }
        asciiTableView.setItems(data);
    }
}
